module Scene {
	/**
	 * 
	 * 游戏场景管理器---刘伟,郭启航
	 */
	export class SceneManager extends egret.DisplayObjectContainer {
		public static SceneManager_sole: SceneManager;
		public constructor() {
			super();
			GameUtil.PlayerMessage.createMsg();
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
			this.scene_prepare = new ScenePrepare();
			this.scene_defense = new SceneDefense();
			this.endGame_ui = new Fighter.GameOverUI();
			this.winGame_ui = new Fighter.WinUI();
			this.Ol_ui = new Fighter.OlUI();
			this.StartUI = new Fighter.StartUI();
			this.eneDefenseUI = new Fighter.EnemyDefenseUI();
			this.eneDefenseLogic = new Fighter.EnemyDefense();

			this.Ai = new Fighter.Ai();
			this.scene_fight = new SceneFight();

			this.addEventListener(egret.Event.ENTER_FRAME, this.updateGameView, this);
			SceneManager.countDown.addEventListener(egret.TimerEvent.TIMER, this.TimeDown, this);
			SceneManager.SceneManager_sole = this;

			//加载子弹配置文件
			let bulletJosn = RES.getRes("BulletConfig_json")
			GameUtil.PlayerMessage.instance.bulletConfig = bulletJosn;
			//加载僚机配置文件
			let guard_planeJson = RES.getRes("GuardPlaneConfig_json");
			GameUtil.PlayerMessage.instance.Guard_PlaneConfig = guard_planeJson;
			//加载敌机配置文件
			let planeJson = RES.getRes("PlaneConfig_json");
			GameUtil.PlayerMessage.instance.clientPlaneConfig = planeJson;

			
		}
		/**计时文本*/
		public static countDown: egret.Timer = new egret.Timer(1000, 0);
		/**游戏准备场景 */
		public scene_prepare: ScenePrepare;
		/**游戏战斗场景 */
		public scene_fight: SceneFight;
		/**游戏防守场景 */
		public scene_defense: SceneDefense;
		/**游戏初始界面 */
		public StartUI: Fighter.StartUI;

		/**敌方防守画面 */
		public eneDefenseUI: Fighter.EnemyDefenseUI;
		/**敌方防守逻辑 */
		public eneDefenseLogic: Fighter.EnemyDefense;

		/**游戏失败界面 */
		private endGame_ui: Fighter.GameOverUI;
		/**游戏胜利界面 */
		private winGame_ui: Fighter.WinUI;

		/**联网模式--by王石 */
		private Ol_ui: Fighter.OlUI;
		/**ai设置 */
		public Ai: Fighter.Ai;
		/**是否接收到服务器游戏结束信息 */
		public is_gameOver = false;
		/**游戏是否胜利 */
		public winNum: number;

		/**倒计时显示*/
		private TimeDown() {
			//console.log("倒计时"+PlayerMsg.instance.time);
			GameUtil.PlayerMessage.createMsg().time -= 1;
		}
		/**场景管理加入舞台 */
		private addToStage(evt: egret.Event) {
			//添加第一波的默认飞机
			this.addFirstEnemy();
			this.addChild(Fighter.StartUI.StartUI_sole);
		}
		/**添加第一波飞机 */
		private addFirstEnemy() {
			GameUtil.PlayerMessage.instance.sendEnemy = [];
			GameUtil.PlayerMessage.instance.getEnemy = [];
			for (let i = 0; i < GameUtil.PlayerMessage.instance.PlaneNum; i++) {
				GameUtil.PlayerMessage.instance.sendEnemy.push(EnemyType["feiji0_png"]);
				GameUtil.PlayerMessage.instance.getEnemy.push(EnemyType["feiji0_png"]);
			}
			for (let i = 0; i < GameUtil.PlayerMessage.instance.getEnemy.length; i++) {
				let point = new egret.Point();
				point.y = 0;
				point.x = Math.random() * 500;
				GameUtil.PlayerMessage.instance.airBattlePosition.push(point);
			}
		}
		/**每帧控制 */
		private updateGameView(evt: egret.Event) {
			//联机界面返回初始页面
			if(Fighter.OlUI.OlUI_solo.backMainPage){
				Fighter.OlUI.OlUI_solo.backMainPage = false;
				this.addChild(Fighter.StartUI.StartUI_sole);
			}

			//游戏初始页面关闭，加载连接模式或单机模式游戏
			if (Fighter.StartUI.StartUI_sole.is_close) {
				SceneManager.countDown.start();
				Fighter.StartUI.StartUI_sole.is_close = false;
				if (Fighter.OlUI.OlUI_solo.is_close) {
					this.removeChild(Fighter.OlUI.OlUI_solo);
					Fighter.OlUI.OlUI_solo.is_close = false;
				} else {
					if (Fighter.StartUI.StartUI_sole.parent)
						Fighter.StartUI.StartUI_sole.parent.removeChild(Fighter.StartUI.StartUI_sole);
				}


				if (GameUtil.PlayerMessage.instance.onePerson) {
					GameUtil.PlayerMessage.instance.planeConfig = GameUtil.PlayerMessage.instance.clientPlaneConfig;
				}
				else {
					GameUtil.PlayerMessage.instance.planeConfig = GameUtil.PlayerMessage.instance.serverPlaneConfig;
					webNetworkMananger.getInstance().send_enemy(0);
				}

				this.addChild(this.scene_fight);

			}
			//游戏空战结束，添加游戏防守与准备界面
			if (this.scene_fight.air_end) {
				//GameUtil.PlayerMessage.instance.limitToBuyEnemy = 1000000;//重新设置购买数量
				//投资回报		
				GameUtil.PlayerMessage.instance.money += GameUtil.PlayerMessage.instance.investment / 10;
				if (!GameUtil.PlayerMessage.instance.onePerson) {
					webNetworkMananger.instance.send_leakEnemy();
				}
				else {
					this.Ai.OneTurn();
					GameUtil.PlayerMessage.instance.EneMissList = GameUtil.PlayerMessage.instance.Aidefense;
				}
				this.scene_fight.air_end = false;
				//this.removeChild(this.scene_fight);
				this.removeChildren();
				this.addChild(this.scene_defense);
				this.addChild(this.eneDefenseUI);
				this.addChild(this.eneDefenseLogic);
				this.addChild(this.scene_prepare);
				GameUtil.PlayerMessage.instance.Game2addEnemy.splice(0, GameUtil.PlayerMessage.instance.Game2addEnemy.length);
				//PlayerMsg.instance.EneMissList.splice(0,PlayerMsg.instance.EneMissList.length);
			}
			//己方防守切换准备界面
			if (this.scene_defense.defenceUI.is_AddPrepareUI) {
				this.scene_defense.defenceUI.is_AddPrepareUI = false;
				this.addChild(this.scene_prepare);
				//this.removeChild(this.scene_defense);
			}
			//准备界面切换己方防守
			if (this.scene_prepare.Prepare_UI.is_AddDefenseUI) {
				this.scene_prepare.Prepare_UI.is_AddDefenseUI = false;
				this.addChild(this.scene_defense);
				//this.removeChild(this.scene_prepare);
			}
			//准备场景切换敌方防守
			if (this.scene_prepare.Prepare_UI.IS_AddEnemyDefenseUI) {
				this.scene_prepare.Prepare_UI.IS_AddEnemyDefenseUI = false;
				this.addChild(this.eneDefenseUI);
				this.addChild(this.eneDefenseLogic);
			}
			//敌方防守切换准备场景
			if (this.eneDefenseUI.IS_AddPrepareUI) {
				this.eneDefenseUI.IS_AddPrepareUI = false;
				this.addChild(this.scene_prepare);

			}
			//进入下一轮准备
			if (this.scene_defense.next_turn) {
				GameUtil.PlayerMessage.instance.game_turns++;
				//重置数量

				//GameUtil.PlayerMessage.instance.PlaneNum=50;
				this.scene_defense.next_turn = false;
				if (!this.scene_fight.$hasAddToStage) {
					this.addChild(this.scene_prepare);
				}
			}
			//准备场景结束，添加游戏战斗场景 
			if (this.scene_prepare.EndPrepare) {
				GameUtil.PlayerMessage.instance.enemyToNext = false;
				GameUtil.PlayerMessage.instance.isReady = false;
				this.scene_prepare.EndPrepare = false;
				GameUtil.PlayerMessage.instance.Game2addEnemy = [];
				GameUtil.PlayerMessage.instance.EneMissList = [];
				GameUtil.PlayerMessage.instance.time = 90;/////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
				this.removeChildren();
				GameUtil.PlayerMessage.instance.AiPlane = [];
				this.Ai.AIPlane();
				if (GameUtil.PlayerMessage.instance.onePerson) {
					GameUtil.PlayerMessage.instance.getEnemy = GameUtil.PlayerMessage.instance.AiPlane;
					GameUtil.PlayerMessage.instance.AiPlane = [];
					for (let i = 0; i < GameUtil.PlayerMessage.instance.getEnemy.length; i++) {
						let point = new egret.Point();
						point.y = 0;
						point.x = Math.random() * 500;
						GameUtil.PlayerMessage.instance.airBattlePosition.push(point);
					}
				}
				GameUtil.PlayerMessage.instance.Game2addEnemy = [];
				GameUtil.PlayerMessage.instance.EneMissList = [];
				GameUtil.PlayerMessage.instance.AllPlaneCount = GameUtil.PlayerMessage.instance.getEnemy.length;

				this.addChild(this.scene_fight);
			}



			//发送游戏失败信息
			if (this.scene_defense.gameOver && !GameUtil.PlayerMessage.instance.onePerson) {
				this.scene_defense.gameOver = false;
				webNetworkMananger.getInstance();
				webNetworkMananger.instance.gameOver();
			}
			//服务器接受游戏结束信息
			if (this.is_gameOver) {
				this.is_gameOver = false;
				GameUtil.PlayerMessage.instance.initialize();
				//	webNetworkMananger.getInstance().webSocket.close();
				this.removeChildren();

				if (this.winNum == 0) {
					//添加游戏失败画面
					this.addChild(this.endGame_ui);
					SceneManager.countDown.stop();
				}
				if (this.winNum == 1) {
					//添加游戏胜利画面
					this.addChild(this.winGame_ui);
					//console.log("加载胜利画面");
					SceneManager.countDown.stop();

				}
				if (this.scene_defense.$hasAddToStage) {
					this.removeChild(this.scene_defense);
				}

			}
			//重新开始游戏
			if (this.endGame_ui.is_restart) {
				this.endGame_ui.is_restart = false;
				this.removeChildren();
				GameUtil.PlayerMessage.instance.initialize();
				this.addFirstEnemy();
				this.addChild(Fighter.StartUI.StartUI_sole);
			}
			if (this.winGame_ui.is_restart) {
				this.winGame_ui.is_restart = false;
				this.removeChildren();
				GameUtil.PlayerMessage.instance.initialize();
				this.addFirstEnemy();
				this.addChild(Fighter.StartUI.StartUI_sole);

			}
			//单人模式
			if (GameUtil.PlayerMessage.instance.onePerson) {
				if (GameUtil.PlayerMessage.instance.time == 0) {
					this.scene_prepare.EndPrepare = true;
				}
				//单人模式游戏结束
				if (this.scene_defense.gameOver) {
					this.scene_defense.gameOver = false;
					this.removeChildren();
					this.addChild(this.endGame_ui);
					SceneManager.countDown.stop();
				}
				if (this.eneDefenseLogic.is_game_Win) {
					this.eneDefenseLogic.is_game_Win = false;
					this.removeChildren();
					this.addChild(this.winGame_ui);
					SceneManager.countDown.stop();
				}
			}


		}

	}
}